<?php
/**
 * FastApp.
 * 12/23/24
 * @Author deepsea
 * @Contact (telegram:deepsea159)
 */

namespace App\Fastapp\Controller;

use App\Fastapp\Model\Account;
use App\Fastapp\Model\AccountInviteLevel;
use App\Fastapp\Model\AccountRecharge;
use App\Fastapp\Model\AccountWallet;
use App\Fastapp\Model\AccountWalletLog;
use App\Fastapp\Model\AccountWithdrawal;
use App\Fastapp\Model\FastappChannel;
use App\Fastapp\Model\FastappChannelAgent;
use Carbon\Carbon;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Mine\Annotation\Auth;
use Mine\Middlewares\CheckModuleMiddleware;
use Mine\MineController;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: "fastapp/dashboard"), Auth]
#[Middleware(middleware: CheckModuleMiddleware::class)]
class FastappDashboardController extends MineController
{
    #[GetMapping('statistics')]
    public function statistics(): ResponseInterface
    {
        $btw = [Carbon::now()->subMonth()->toDateString(), Carbon::now()->addDay()->toDateString()];
        $btw2 = [Carbon::now()->toDateString(), Carbon::now()->addDay()->toDateString()];
        $total_account_num = Account::query()->count();
        $total_account_month_num = Account::query()->whereBetween('created_at', $btw)->count();
        $total_account_day_num = Account::query()->whereBetween('created_at', $btw2)->count();

        $total_recharge_amount = AccountRecharge::query()->where(['status' => 1])->sum('amount');
        $total_recharge_month_amount = AccountRecharge::query()->where(['status' => 1])->whereBetween('created_at', $btw)->sum('amount');
        $total_recharge_day_amount = AccountRecharge::query()->where(['status' => 1])->whereBetween('created_at', $btw2)->sum('amount');
        $total_recharge_pump = AccountRecharge::query()->where(['status' => 1])->sum('pump');


        $total_withdrawal_amount = AccountWithdrawal::query()->where(['status' => 1])->sum('amount');
        $total_withdrawal_month_amount = AccountWithdrawal::query()->where(['status' => 1])->whereBetween('created_at', $btw)->sum('amount');
        $total_withdrawal_day_amount = AccountWithdrawal::query()->where(['status' => 1])->whereBetween('created_at', $btw2)->sum('amount');
        $total_withdrawal_pump = AccountWithdrawal::query()->where(['status' => 1])->sum('pump');


        $total_wallet_balance = AccountWallet::query()
            ->selectRaw('sum(balance)as balance,symbol')->groupBy('symbol')
            ->get(['balance', 'symbol'])->mapWithKeys(function ($item) {
                return [$item->symbol => nfmt($item->balance)];
            });


        return $this->success([
            'total_account_num' => $total_account_num,
            'total_account_month_num' => $total_account_month_num,
            'total_account_day_num' => $total_account_day_num,

            'total_recharge_amount' => nfmt($total_recharge_amount),
            'total_recharge_month_amount' => nfmt($total_recharge_month_amount),
            'total_recharge_day_amount' => nfmt($total_recharge_day_amount),
            'total_recharge_pump' => nfmt($total_recharge_pump),

            'total_withdrawal_amount' => nfmt($total_withdrawal_amount),
            'total_withdrawal_month_amount' => nfmt($total_withdrawal_month_amount),
            'total_withdrawal_day_amount' => nfmt($total_withdrawal_day_amount),
            'total_withdrawal_pump' => nfmt($total_withdrawal_pump),

            'total_wallet_balance' => $total_wallet_balance
        ]);
    }

    #[GetMapping('channel')]
    public function channel(): ResponseInterface
    {
        $map['channel'] = FastappChannel::query()->where(['admin_id' => user()->getId()])->value('id');
        $btw = [Carbon::now()->subMonth()->toDateString(), Carbon::now()->addDay()->toDateString()];
        $btw2 = [Carbon::now()->toDateString(), Carbon::now()->addDay()->toDateString()];
        $total_account_num = Account::query()->where($map)->count();
        $total_account_month_num = Account::query()->where($map)->whereBetween('created_at', $btw)->count();
        $total_account_day_num = Account::query()->where($map)->whereBetween('created_at', $btw2)->count();

        $map['type'] = AccountWalletLog::RECHARGE;
        $total_recharge_amount = AccountWalletLog::query()->where($map)->sum('amount');
        $total_recharge_month_amount = AccountWalletLog::query()->where($map)->whereBetween('created_at', $btw)->sum('amount');
        $total_recharge_day_amount = AccountWalletLog::query()->where($map)->whereBetween('created_at', $btw2)->sum('amount');

        $map['type'] = AccountWalletLog::WITHDRAWAL;
        $total_withdrawal_amount = AccountWalletLog::query()->where($map)->sum('amount');
        $total_withdrawal_month_amount = AccountWalletLog::query()->where($map)->whereBetween('created_at', $btw)->sum('amount');
        $total_withdrawal_day_amount = AccountWalletLog::query()->where($map)->whereBetween('created_at', $btw2)->sum('amount');

        $total_wallet_balance = AccountWallet::query()
            ->selectRaw('sum(balance)as balance,symbol')->groupBy('symbol')
            ->where(['channel' => $map['channel']])
            ->get(['balance', 'symbol'])->mapWithKeys(function ($item) {
                return [$item->symbol => nfmt($item->balance)];
            });

        return $this->success([
            'total_account_num' => $total_account_num,
            'total_account_month_num' => $total_account_month_num,
            'total_account_day_num' => $total_account_day_num,

            'total_recharge_amount' => nfmt($total_recharge_amount),
            'total_recharge_month_amount' => nfmt($total_recharge_month_amount),
            'total_recharge_day_amount' => nfmt($total_recharge_day_amount),

            'total_withdrawal_amount' => nfmt($total_withdrawal_amount),
            'total_withdrawal_month_amount' => nfmt($total_withdrawal_month_amount),
            'total_withdrawal_day_amount' => nfmt($total_withdrawal_day_amount),

            'total_wallet_balance' => $total_wallet_balance
        ]);
    }

    #[GetMapping('agent')]
    public function agent(): ResponseInterface
    {
        $agent_user_id = FastappChannelAgent::query()->where(['admin_id' => user()->getId()])->value('user_id');
        $btw = [Carbon::now()->subMonth()->toDateString(), Carbon::now()->addDay()->toDateString()];
        $btw2 = [Carbon::now()->toDateString(), Carbon::now()->addDay()->toDateString()];
        $link = AccountInviteLevel::query()->where(['user_id' => $agent_user_id])->value('link');
        if ($link) $link .= '.' . $agent_user_id . '%'; else  $link = $agent_user_id . '|%';
        $total_account_num = AccountInviteLevel::query()->where('link', 'like', $link)->count();
        $total_account_month_num = AccountInviteLevel::query()->where('link', 'like', $link)->whereBetween('created_at', $btw)->count();
        $total_account_day_num = AccountInviteLevel::query()->where('link', 'like', $link)->whereBetween('created_at', $btw2)->count();

        $query = AccountInviteLevel::query()
            ->from('account_invite_level as a')
            ->leftJoin('account_wallet_log as b', 'a.user_id', '=', 'b.user_id');

        $map['type'] = AccountWalletLog::RECHARGE;
        $total_recharge_amount = $query->where($map)->where('link', 'like', $link)->sum('amount');
        $total_recharge_month_amount = $query->where($map)->where('link', 'like', $link)->whereBetween('a.created_at', $btw)->sum('amount');
        $total_recharge_day_amount = $query->where($map)->where('link', 'like', $link)->whereBetween('a.created_at', $btw2)->sum('amount');

        $map['type'] = AccountWalletLog::WITHDRAWAL;
        $total_withdrawal_amount = $query->where($map)->where('link', 'like', $link)->sum('amount');
        $total_withdrawal_month_amount = $query->where($map)->where('link', 'like', $link)->whereBetween('a.created_at', $btw)->sum('amount');
        $total_withdrawal_day_amount = $query->where($map)->where('link', 'like', $link)->whereBetween('a.created_at', $btw2)->sum('amount');

        $total_wallet_balance = AccountInviteLevel::query()
            ->from('account_invite_level as a')
            ->leftJoin('account_wallet as b', 'a.user_id', '=', 'b.user_id')
            ->where('link', 'like', $link)
            ->selectRaw('sum(balance)as balance,symbol')->groupBy('symbol')
            ->get(['balance', 'symbol'])->mapWithKeys(function ($item) {
                return [$item->symbol => nfmt($item->balance)];
            });

        return $this->success([
            'total_account_num' => $total_account_num,
            'total_account_month_num' => $total_account_month_num,
            'total_account_day_num' => $total_account_day_num,

            'total_recharge_amount' => nfmt($total_recharge_amount),
            'total_recharge_month_amount' => nfmt($total_recharge_month_amount),
            'total_recharge_day_amount' => nfmt($total_recharge_day_amount),

            'total_withdrawal_amount' => nfmt($total_withdrawal_amount),
            'total_withdrawal_month_amount' => nfmt($total_withdrawal_month_amount),
            'total_withdrawal_day_amount' => nfmt($total_withdrawal_day_amount),

            'total_wallet_balance' => $total_wallet_balance
        ]);
    }

    #[GetMapping('userRetentionCurve')]
    public function userRetentionCurve()
    {

    }
}